package com.dark.cms.base.dto;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaseDTO extends HashMap{

	/**
	 * 重写put方法,struts自动填充的是一个数组
	 */
	public Object put(Object key, Object v){
		if(v instanceof String[]){
			String[] tmp = (String[])v;
			if(tmp != null && tmp.length == 1){
				return super.put(key, tmp[0]);
			}
		}
		return super.put(key, v);
	}
	
	public String getAsString(String key) {
		String str = "";
		if (this.get(key)!=null) {
			str = this.get(key).toString();
		}
		return str;
	}

	public Date getAsDate(String key) {
		Date date = null;
		if (this.get(key)!=null) {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			try {
				date = format.parse(this.get(key).toString());
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return date;
	}

	public Date getAsTime(String key) {
		Date date = null;
		if (this.get(key)!=null) {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				date = format.parse(this.get(key).toString());
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return date;
	}
	
	public Integer getAsInteger(String key) {
		Integer i = 0;
		if (this.get(key)!=null) {
			i = Integer.parseInt(this.get(key).toString());
		}
		return i;
	}

	public Double getAsDouble(String key) {
		Double i = 0d;
		if (this.get(key)!=null) {
			i = Double.parseDouble(this.get(key).toString());
		}
		return i;
	}

	public Float getAsFloat(String key) {
		Float i = 0f;
		if (this.get(key)!=null) {
			i = Float.parseFloat(this.get(key).toString());
		}
		return i;
	}

	public Boolean getAsBoolean(String key) {
		Boolean i = false;
		if (this.get(key)!=null) {
			i = Boolean.parseBoolean(this.get(key).toString());
		}
		return i;
	}

	public List getAsList(String key) {
		List list = null;
		if (this.get(key)!=null) {
			list = (List) this.get(key);
		}
		return list;
	}

	public String[] getAsArray(String key) {
		String[] list = null;
		if (this.get(key)!=null) {
			list = (String[]) this.get(key);
		}
		return list;
	}

	public Map getAsMap(String key) {
		Map hm = null;
		if (this.get(key)!=null) {
			hm = (Map) this.get(key);
		}
		return hm;
	}

	public BaseDTO clone() {
		BaseDTO dto = (BaseDTO) this.clone();
		return dto;
	}

	public void setValue(String key, Object value) {
		this.put(key, value);
	}

	public void setValue(Map hm) {
		this.putAll(hm);
	}

	public BaseDTO append(Object key, Object value){
		this.put(key, value);
		return this;
	}
}
